package com.common.sdk.dto;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * @author : YU.TAN
 * @date : 2021/12/6 16:14
 */
@Data
@NoArgsConstructor
@JsonIgnoreProperties(ignoreUnknown = true)
@ApiModel(value = "基础API返回对象")
@JsonInclude(JsonInclude.Include.NON_NULL)
@AllArgsConstructor
public class BaseFxResponse<T> {


    @ApiModelProperty(value = "处理结果code", required = true)
    private int code = ResultCode.SUCCESS.getCode();
    @ApiModelProperty(value = "处理结果描述信息")
    private String msg = ResultCode.SUCCESS.getMessage();
    @ApiModelProperty(value = "请求结果生成时间戳")
    private String timestamp;
    @ApiModelProperty(value = "处理结果数据信息")
    private T data;


    public BaseFxResponse(int code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    /**
     * 1、内部使用，用于构造成功的结果
     * 2、对接外部系统错误码及消息
     *
     * @param code
     * @param msg
     * @param data
     */
    public BaseFxResponse(int code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    /**
     * 快速创建成功结果并返回结果数据
     *
     * @param data
     * @return ApiResult
     */
    public static <T> BaseFxResponse<T> success(T data) {
        return new BaseFxResponse<>(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMessage(), data);
    }

    /**
     * 快速创建成功结果
     *
     * @return ApiResult
     */
    public static <T> BaseFxResponse<T> success() {
        return success(null);
    }

    /**
     * 系统异常类没有返回数据
     *
     * @return BaseFxResponse
     */
    public static <T> BaseFxResponse<T> fail() {
        return new BaseFxResponse(ResultCode.EXCEPTION.getCode(), ResultCode.EXCEPTION.getMessage(), null);
    }

    /**
     * 快速创建失败结果并返回结果数据
     *
     * @return BaseFxResponse
     */
    public static <T> BaseFxResponse<T> fail(T data) {
        return new BaseFxResponse(ResultCode.FAIL.getCode(), ResultCode.FAIL.getMessage(), data);
    }

    /**
     * 成功code=000000
     *
     * @return true/false
     */
    @JsonIgnore
    public static boolean isSuccess(int code) {
        return ResultCode.SUCCESS.getCode() == code;
    }

    /**
     * 快速创建成功结果并返回结果数据
     *
     * @param msg
     * @return ApiResult
     */
    public static  <T> BaseFxResponse<T> failMsg(String msg) {
        return new BaseFxResponse<>(ResultCode.FAIL.getCode(), msg, null);
    }


    public static  <T>  BaseFxResponse<T> fail(ResultCode resultCode) {
        return new BaseFxResponse(resultCode.getCode(), resultCode.getMessage(), null);
    }

    public static BaseFxResponse<Object> result(int code, String msg) {
        return new BaseFxResponse<>(code, msg, null);
    }
}
